using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using PoserLoot.Web.Models;

namespace PoserLoot.Web.Controllers
{
    public class FileController : BaseController
    {
        [Authorize(Roles = "Administrators")]
        public ActionResult Create(Guid id)
        {
            var entry = _service.GetEntry(id);
            entry.CategoryReference.Load();

            ViewData["CategoryId"] = entry.Category.CategoryId;
            ViewData["EntryId"] = id;
            ViewData["EntryAppellation"] = entry.Appellation;

            ViewData["IEnumerable<Category>"] = _service.ListCategories();

            return View();
        }

        [Authorize(Roles = "Administrators")]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Guid id, FormCollection collection)
        {
            File file = new File()
            {
                Path = collection["Path"],
                Entry = _service.GetEntry(id)
            };

            if (_service.CreateFile(file))
                return RedirectToAction("Details", "Entry", new { id = id });

            file.Entry.CategoryReference.Load();

            ViewData["CategoryId"] = file.Entry.Category.CategoryId;
            ViewData["EntryId"] = id;
            ViewData["EntryAppellation"] = file.Entry.Appellation;

            ViewData["IEnumerable<Category>"] = _service.ListCategories();

            return View();
        }

        [Authorize(Roles = "Administrators")]
        public ActionResult Delete(Guid id)
        {
            var file = _service.GetFile(id);
            file.EntryReference.Load();
            Guid entryId = file.Entry.EntryId;

            _service.DeleteFile(id);

            return RedirectToAction("Details", "Entry", new { id = entryId });
        }
    }
}
